package com.stx.crazyblog.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.crazyblog.entity.Category;
import com.stx.crazyblog.dao.CategoryDao;
/**
 * 添加分类内容的servlet方法
 * 
 * */
public class CategoryAddServlet extends HttpServlet {
//	保证版本升级时对象的唯一性
	private static final long serialVersionUID = 1L;
//	初始化分类dao对象
	CategoryDao dao = null;
//	将评论类dao对象放入servlet中
	public void init(){
//		初始化dao对象
		dao = new CategoryDao();
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request,
	 *				HttpServletResponse response)
	 * 
	 * */		
			protected void doGet(HttpServletRequest request,
					HttpServletResponse response)
							throws ServletException,IOException{
				doPost(request,response);
			}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request,
	 *						HttpServletResponse response)
	 * 
	 * */		
			protected void doPost(HttpServletRequest request,
					HttpServletResponse response)
							throws ServletException,IOException{
//				定义编码为“UTF-8” 防止中文出现乱码
				request.setCharacterEncoding("utf-8");
//				创建分类数据视图对象（用于封装分类数据内容）
				Category categoryParams = new Category();
//				从页面获取新增数据
				categoryParams.setCid(request.getParameter("ca_cid"));
				categoryParams.setCname(request.getParameter("ca_cname"));
				categoryParams.setUid(request.getParameter("us_uid"));
				categoryParams.setCreatetime(request.getParameter("ca_createtime"));
				
				try{
//					从dao中调入插入分类数据的方法
					dao.insertCategory(categoryParams);
//					将结果保存到request中
					request.setAttribute("message", "分类成功");
//					调用request中的查询方法，将分类表的内容更新
					RequestDispatcher rd = getServletContext().getRequestDispatcher("/CategoryAddServlet");
//					将请求转发给servlet
					rd.forward(request, response);
				}catch(Exception e){
//					将结果保存到request中
					request.setAttribute("message", "分类失败");
//					调用request中的查询方法，将分类表的内容更新
					RequestDispatcher rd = getServletContext().getRequestDispatcher("/CategoryAddServlet");
//					将请求转发给servlet
					rd.forward(request, response);					
				}
			}	
}
